package com.meishij.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.meishij.MallZcActivity;
import com.meishij.entry.MallEntry;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/12.
 */

public class MallViewPagerAdapter extends PagerAdapter {
    private Context context;
    private List<MallEntry.BuyIndexTopBean> buyIndexTopBeen;
    private Pools.Pool<SimpleDraweeView> pool=new Pools.SimplePool<>(4);
    public MallViewPagerAdapter(Context context, List<MallEntry.BuyIndexTopBean> list) {
        this.context = context;
        this.buyIndexTopBeen = list;
        MallEntry.BuyIndexTopBean first = list.get(0);
        MallEntry.BuyIndexTopBean last = list.get(list.size() - 1);
        this.buyIndexTopBeen.add(0,last);
        this.buyIndexTopBeen.add(first);
    }

    @Override
    public int getCount() {
        return buyIndexTopBeen.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        SimpleDraweeView simpleDraweeView = pool.acquire();
        if(simpleDraweeView==null){
            simpleDraweeView=new SimpleDraweeView(context);
        }
        simpleDraweeView.setImageURI(buyIndexTopBeen.get(position).getPhoto());
        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String jump = buyIndexTopBeen.get(position).getJump();//jump是一个json数据
                String json = new GsonBuilder().disableHtmlEscaping().create().toJson(jump);
                String s = json.replace("\\","");//将其中的\去掉
                String substring = s.substring(1, s.length() - 1);//再把最前面和最后面的"去掉
                try {
                    JSONObject jsonObject = new JSONObject(substring);
                    JSONObject property = jsonObject.getJSONObject("property");
                    String urlString = property.getString("urlString");//这里拿到的就是我们需要的了
                    Intent intent=new Intent(context, MallZcActivity.class);
                    intent.putExtra("url",urlString);
                    context.startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });
        container.addView(simpleDraweeView);
        return simpleDraweeView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release((SimpleDraweeView) object);
    }
}
